package com.haixiaoke.business;

import com.haixiaoke.common.core.domain.AjaxResult;
import com.haixiaoke.common.core.page.TableDataInfo;
import com.haixiaoke.common.utils.poi.ExcelUtil;
import com.haixiaoke.domain.SmartLock;
import com.haixiaoke.domain.SmartLockCard;
import com.haixiaoke.service.ISmartLockCardService;
import com.haixiaoke.service.ISmartLockService;
import com.haixiaoke.smartLock.haixiaoke.SmartLockHXKEventHandleService;
import com.haixiaoke.smartLock.huixiangjia.SmartLockHxjEventHandleService;
import com.haixiaoke.smartLock.tianwanghulian.SmartLockTwhlEventHandleService;
import com.haixiaoke.smartLock.yaluete.SmartLockYLTEventService;
import com.haixiaoke.smartLock.youdian.SmartLockYdEventHandleService;
import com.haixiaoke.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 智能锁门卡授权信息Controller
 *
 * @author xiaolong
 * @date 2023-05-31
 */
@RestController
@RequestMapping("/business/card")
public class SmartLockCardController extends BaseController {
    @Autowired
    private ISmartLockCardService smartLockCardService;
    @Autowired
    private ISmartLockService smartLockService;
    @Autowired
    private SmartLockTwhlEventHandleService smartLockTwhlEventHandleService;
    @Autowired
    private SmartLockYLTEventService smartLockYLTEventService;
    @Autowired
    private SmartLockHXKEventHandleService smartLockHXKEventHandleService;
    @Autowired
    private SmartLockYdEventHandleService smartLockYdEventHandleService;
    @Autowired
    private SmartLockHxjEventHandleService smartLockHxjEventHandleService;

    /**
     * 查询智能锁门卡授权信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SmartLockCard smartLockCard) {
        startPage();
        List<SmartLockCard> list = smartLockCardService.selectSmartLockCardList(smartLockCard);
        return getDataTable(list);
    }

    /**
     * 导出智能锁门卡授权信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmartLockCard smartLockCard) {
        List<SmartLockCard> list = smartLockCardService.selectSmartLockCardList(smartLockCard);
        ExcelUtil<SmartLockCard> util = new ExcelUtil<SmartLockCard>(SmartLockCard.class);
        util.exportExcel(response, list, "智能锁门卡授权信息数据");
    }

    /**
     * 获取智能锁门卡授权信息详细信息
     */
    @GetMapping(value = "/{cardId}")
    public AjaxResult getInfo(@PathVariable("cardId") Long cardId) {
        return success(smartLockCardService.selectSmartLockCardByCardId(cardId));
    }

    /**
     * 新增智能锁门卡授权信息和授权门卡
     */
    @PostMapping
    public AjaxResult add(@RequestBody SmartLockCard smartLockCard) {
        SmartLock smartLock = smartLockService.selectBySn(smartLockCard.getSn());
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYLTEventService.insertSmartLockCard(smartLockCard));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return toAjax(smartLockTwhlEventHandleService.insertSmartLockCard(smartLockCard));
        } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHXKEventHandleService.insertSmartLockCard(smartLockCard));
        } else if (smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK){
            return toAjax(smartLockYdEventHandleService.insertSmartLockCard(smartLockCard));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK){
            return toAjax(smartLockHxjEventHandleService.insertSmartLockCard(smartLockCard));
        } else {
            return error("暂不支持该品牌的智能锁");
        }
    }

    /**
     * 修改智能锁门卡授权信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SmartLockCard smartLockCard) {
        return toAjax(smartLockCardService.updateSmartLockCard(smartLockCard));
    }

    /**
     * 删除智能锁门卡授权信息
     */
    @DeleteMapping("/{cardIds}")
    public AjaxResult remove(@PathVariable Long[] cardIds) {
        return toAjax(smartLockCardService.deleteSmartLockCardByCardIds(cardIds));
    }

    /**
     * 删除智能锁门卡授权信息和授权门卡授权
     *
     * @return
     */
    @DeleteMapping("/remove")
    public AjaxResult remove(SmartLockCard smartLockCard) {
        String sn = smartLockCard.getSn();
        String identification = smartLockCard.getIdentification();
        SmartLock smartLock = smartLockService.selectBySn(sn);
        if (smartLock.getBrandId() == SmartLock.THIRD_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYLTEventService.deleteSmartLockCard(smartLockCard));
        } else if (smartLock.getBrandId() == SmartLock.FOUR_GENERATION_SMART_LOCK) {
            return toAjax(smartLockTwhlEventHandleService.deleteSmartLockCardBySnAndIdentification(sn, identification));
        } else if (smartLock.getBrandId() == SmartLock.FIVE_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHXKEventHandleService.deleteSmartLockCard(smartLockCard));
        } else if (smartLock.getBrandId() == SmartLock.SIX_GENERATION_SMART_LOCK) {
            return toAjax(smartLockYdEventHandleService.deleteSmartLockCard(smartLockCard));
        } else if (smartLock.getBrandId() == SmartLock.SEVEN_GENERATION_SMART_LOCK) {
            return toAjax(smartLockHxjEventHandleService.deleteSmartLockCard(smartLockCard));
        } else {
            return error("暂不支持该品牌的智能锁");
        }
    }
}
